
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SurveyHost.Core;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.ComponentModel;
using SurveyHost.Core.Controls;
using SurveyHost.Core.DAL;
//using SurveyHost.Core.DAL.SurveyDataTableAdapters;

namespace SurveyHost.Web.Participant
{
    public partial class Survey : System.Web.UI.Page
    {
        private SurveyManager Manager
        {
            get { return SurveyManager.SessionInstance; }
        }
        /// <summary>
        /// The instructions for the current measure
        /// </summary>
        public string MeasureInstructions
        {
            get
            {
                if (Manager.CurrentMeasure == null
                    || Manager.CurrentMeasure.Instructions == null)
                    return string.Empty;

                return Manager.CurrentMeasure.Instructions;
            }
        }

        /// <summary>
        /// True if there are instructions to show, false otherwise.
        /// </summary>
        public bool ShowInstructions
        {
            get
            {
                if (User.IsInRole("Admin")) return true;
                if (Manager.CurrentMeasure == null
                    || Manager.CurrentMeasure.Instructions == null)
                    return false;
                return true;
            }
        }

        #region  Methods (5)
        protected void Page_Init(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Guid g = WebUtil.CurrentUserId();
                if (!User.IsInRole("Admin"))
                {
                    if (StatusHelper.IsCompleted(g) && !StatusHelper.UserIsValid(g))
                        Response.Redirect("NonParticipating.aspx", true);
                    if (!StatusHelper.UserIsValid(g))
                        Response.Redirect("ConsentForm.aspx", true);
                    if (StatusHelper.IsCompleted(g))
                        Response.Redirect("SurveyComplete.aspx", true);
                    if (Manager.CurrentMeasure == null) // refreshing the page = !IsPostback
                        Manager.NextMeasure();
                }

            }
            QuestionList1.DataSource = Manager.CurrentQuestions;
            QuestionList1.DataBind();
        }

        public void SubmitClick(object sender, EventArgs e)
        {
            if (!IsValid) return;
            bool notAdmin = !User.IsInRole("Admin");
            if (notAdmin)
            {
                foreach (QuestionLineItem item in QuestionList1.QuestionLineItems)
                {
                    Manager.AddAnswer(new Answer
                        {
                            SurveyId = Manager.CurrentSurvey.SurveyId,
                            StemId = item.Definition.StemId,
                            AnswerText = item.QuestionControl.AnswerText
                        });
                }
            }
            // move to the next measure and databind or
            if (Manager.NextMeasure())
            {
                QuestionList1.DataSource = Manager.CurrentQuestions;
                QuestionList1.DataBind();
                return;
            }
            // if we're at the end, save and bail.
            if (notAdmin)
            {
                Manager.CompleteSurvey();
                StatusHelper.MarkCompleted(WebUtil.CurrentUserId());
            }
            Response.Redirect("SurveyComplete.aspx");
        }

        #endregion

    }
}
